package com.jalarbee.web.jsf.controller;

import com.jalarbee.stock.category.business.Category;
import com.jalarbee.stock.category.service.CategoryService;
import java.util.Date;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@RequestScoped
public class CategoryController {

    @Inject
    private CategoryService categoryService;
    private Category category;

    public CategoryController() {
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public Category getCategory() {
        if(category == null)
            initCategory();
        return category;
    }
    
    private void initCategory() {
        category = new Category();
    }

    public String add() {
        try {
            category.setCreationDate(new Date());
            categoryService.create(category);
            initCategory();
            return Navigation.SUCCESS;
        } catch (Exception e) {
            return Navigation.ERROR;
        }
    }
}